﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SouthApps.MobileHealth.SDK.DAL.Model;
using System.Data.Objects;

namespace SouthApps.MobileHealth.SDK.DAL.DataAccess
{
    public class PlatformLicenseDao : DataAccessObject
    {
        #region Add

        public void AddPlatformLicense(PlatformLicense platformLicense)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    entities.AddToplatformlicense(platformLicense);

                    this.SaveChanges(entities);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion

        #region Modify

        public void ModifyPlatformLicense(PlatformLicense platformLicense)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    PlatformLicense oldPlatformLicense = entities.platformlicense.FirstOrDefault(e => e.platformLicenseId == platformLicense.platformLicenseId);
                    oldPlatformLicense = platformLicense;

                    this.SaveChanges(entities);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion

        #region Get

        public List<PlatformLicense> GetEntityPlatformLicenses(int licensedEntityId)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    var query = from e in entities.platformlicense
                                where e.licensedEntityId == licensedEntityId
                                select e;

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        public PlatformLicense GetEntityPlatformLicense(int financiadorId, int platformId)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    return entities.platformlicense.FirstOrDefault(e =>
                                e.licensedEntityId == financiadorId && e.platformId == platformId);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion
    }
}
